package com.example.vue20220402.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220402.pojo.NodeMenu;
import com.example.vue20220402.pojo.Power;
import com.example.vue20220402.pojo.Usersinfo;
import com.example.vue20220402.dao.UsersinfoMapper;
import com.example.vue20220402.service.IUsersinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vue20220402.util.ExcelUtil;
import com.example.vue20220402.vo.Constant;
import com.example.vue20220402.vo.ResultData;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-04-02
 */
@Service
public class UsersinfoServiceImpl implements IUsersinfoService {

    @Resource
    UsersinfoMapper usersinfoMapper;

    @Override
    public Map<String, Object> userlist() {
        Map<String, Object> map  = new HashMap<>();
        QueryWrapper<Usersinfo> q = new QueryWrapper();//  查询工具类
        List<Usersinfo> usersinfos = usersinfoMapper.selectList(q);
        map.put("list",usersinfos);
        return map;
    }

    @Resource
    RedisTemplate redisTemplate;
    @Override
    public Map<String, Object> login(Usersinfo usersinf) {
        //  第一种方式
        Map<String, Object> map  = new HashMap<>();
        QueryWrapper<Usersinfo> q = new QueryWrapper();
        q.eq("username",usersinf.getUsername());
        q.eq("pwd",usersinf.getPwd());
        Usersinfo usersinfo = usersinfoMapper.selectOne(q);
        // 第二种方式
        //Usersinfo usersinfo = usersinfoMapper.login(usersinf);
        if(usersinfo == null){
            map.put("message","登录失败");
        }else{
            map.put("message","登录成功");
            map.put("user",usersinfo);
            //  获取 用户对于的菜单
            List<Power> list = usersinfoMapper.selectPowerList(usersinfo.getId());
            //  把菜单  按照前台格式  进行  一二级分离
            List<NodeMenu> nodes = new ArrayList<>();
            //  添加一级菜单
            for(Power p : list){
                if(p.getPowerIndex() == 1){ //  =1 一级菜单
                    NodeMenu n = new NodeMenu(); //  一级菜单 powerid  powername
                    n.setPowerId(p.getPowerId());
                    n.setPowerName(p.getPowerName());
                    nodes.add(n);
                }
                //System.out.println(p);
            }
            for(NodeMenu n : nodes){
                System.out.println(n); // 输出一级菜单
            }
            //  添加二级菜单
            for(Power p : list){
                for(NodeMenu n : nodes){ //  菜单的父id  =  一级菜单的 主键id
                    if(p.getPowerParentid() == n.getPowerId()){
                        n.getPowers().add(p);
                    }
                }
            }
            for(NodeMenu n : nodes){
                for(Power p : n.getPowers()){
                    System.out.println(p); // 输出二级菜单
                }
            }
            //System.out.println(nodes);
            map.put("nodes",nodes);
            //redisTemplate.opsForValue().set(usersinf.getUsername()+"user",usersinfo);
        }
        return map;
    }

    @Override
    public Map<String, Object> register(Usersinfo usersinf) {
        Map<String, Object> map = new HashMap<>();
        //  需要 查询 用户名重名
        QueryWrapper<Usersinfo> q = new QueryWrapper<>();
        q.eq("username",usersinf.getUsername());
        Usersinfo usersinfo = usersinfoMapper.selectOne(q);
        if(usersinfo != null){
            map.put("message","用户名重名了");
            return map;
        }
        //  插入用户数据
        usersinf.setRoleId(3);// 角色  自动为患者  后期管理员页面更改
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        usersinf.setCreatetime(sd.format(new Date()));
        usersinf.setStats(1);// 默认可用
        usersinfoMapper.insert(usersinf);
        map.put("message","注册成功");
        return map;
    }

    @Override
    public ResultData list(Usersinfo usersinfo) {
        ResultData rd = new ResultData();
        try{
            QueryWrapper<Usersinfo> qw = new QueryWrapper();
            if(usersinfo.getUsername() != null && !usersinfo.getUsername().equals("")){
                qw.like("u.username",usersinfo.getUsername());
            }
            qw.orderByAsc("id");
            Page<Usersinfo> page = new Page<>(usersinfo.getPage(),usersinfo.getRow());
            //List<Usersinfo> list = usersinfoMapper.selectList(qw);
            IPage<Usersinfo> list = usersinfoMapper.selectUserList(page, qw);
            rd.setResult(list);
            rd.setMessage(Constant.USERLIST.getDesc());
            rd.setCode(Constant.OK.getCode());
        }catch (Exception e)
        {
            e.printStackTrace();
            rd.setCode(Constant.ERROR.getCode());
            rd.setMessage(Constant.ERROR.getDesc());
        }
        return rd;
    }

    @Override
    public ResultData updateUser(Usersinfo usersinf) {
        QueryWrapper<Usersinfo> qw = new QueryWrapper();
        qw.eq("id",usersinf.getId());
        int flag = usersinfoMapper.update(usersinf, qw);
        ResultData rd = new ResultData();
        if(flag > 0){
            rd.setCode(Constant.OK.getCode());
        }else{
            rd.setCode(Constant.ERROR.getCode());
        }
        return rd;
    }

    @Override
    public ResultData deleteUser(Usersinfo usersinf) {
        ResultData rd = new ResultData();
        int flag = usersinfoMapper.deleteById(usersinf.getId());
        if(flag > 0){
            rd.setCode(Constant.OK.getCode());
        }else{
            rd.setCode(Constant.ERROR.getCode());
        }
        return rd;
    }

    @Override
    public void exportUser(HttpServletResponse response) {
        String fileName = "用户列表"+System.currentTimeMillis()+".xlsx";
        List<String> columnList = Arrays.asList("编号","用户名","密码","状态","创建时间","角色名称");
        Page<Usersinfo> page = new Page<>(1,10000);
        QueryWrapper<Usersinfo> qw = new QueryWrapper();
        IPage<Usersinfo> list = usersinfoMapper.selectUserList(page, qw);
        List<Usersinfo> dataList = list.getRecords();
        ExcelUtil.uploadExcelAboutUser2(response,fileName,columnList,dataList);
    }
}
